package gui;

import jahuwaldt.plot.Plot;

import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.Vector;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public class TextEditorPanel extends JPanel {
	public TextEditorPanel() {
		super();
		this.initialize();
	}

	private void initialize() {
		this.setLayout(new BorderLayout());
		this.desktop = new JDesktopPane();
		this.add(this.desktop);
		this.desktop.setBackground(Color.GRAY);
		this.inTextPanes = new Vector<JInternalFrame>();
	}

	public void addInternalFrame() {
		InternalTextPane temp = new InternalTextPane("new file");
		this.inTextPanes.add(temp);
		this.desktop.add(temp);
		try {
			temp.setSelected(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addInternalPlotFrame(Plot plot) {
		InternalPlotPane temp = new InternalPlotPane(plot);
		// InternalDBBrowserPane temp = new InternalDBBrowserPane("new file");
		this.inTextPanes.add(temp);
		this.desktop.add(temp);
		try {
			temp.setSelected(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addInternalFrame(File file) {
		InternalTextPane temp = new InternalTextPane(file);
		this.inTextPanes.add(temp);
		this.desktop.add(temp);
		try {
			temp.setSelected(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addInternalDBBrowserFrame(File file) {
		InternalDBBrowserPane temp = new InternalDBBrowserPane(file);
		this.inTextPanes.add(temp);
		this.desktop.add(temp);
		try {
			temp.setSelected(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addInternalFrame(String title, String text) {
		InternalTextPane temp = new InternalTextPane(title);
		temp.setText(text);
		this.inTextPanes.add(temp);
		this.desktop.add(temp);
		try {
			temp.setSelected(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public InternalTextPane getSelectedPane() {
		for (JInternalFrame itPane : this.desktop.getAllFrames()) {
			if (itPane.isSelected()) {
				return (InternalTextPane) itPane;
			}
		}
		return null;
	}

	private JDesktopPane desktop = null;
	private Vector<JInternalFrame> inTextPanes = null;
}
